<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <dad></dad>
  </div>

  <template id="dad">
    <div>
      {{count}}
      <son @addtwo='increment' @subone='decrement' />
    </div>
  </template>
  <template id="son">
    <div>
      <button @click='sub'>-</button>
      <button @click='add'>+</button>
    </div>
  </template>

  <script src="../../vue.js"></script>
  <script>
    Vue.component('dad', {
      template: '#dad',
      data() {
        return {
          count: 0
        }
      },
      methods: {
        increment(val) {
          this.count += val
        },
        decrement(val) {
          this.count -= val
        }
      }
    })

    Vue.component('son', {
      template: '#son',
      methods: {
        add() {
          // 触发自定义事件
          this.$emit('addtwo', 2)
        },
        sub() {
          this.$emit('subone', 1)
        }
      }
    }
    )

    new Vue({
      el: '#app',
    })
  </script>
</body>

</html>